package com.lsh.airticket.controller.admin;

import com.lsh.airticket.dto.Result;
import com.lsh.airticket.entity.City;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lsh
 * @date 2021/2/17
 **/


@RestController
@RequestMapping("/flights")
public class AdminCityController {

    @Autowired
    CityService cityService;

    @GetMapping("/admin/city/cityList")
    public Result findCityList() {
        List<City> cityList = cityService.findAllCity();
        return Result.result().addData("cityList", cityList);
    }


    @PostMapping("/admin/city/addCity")
    public Result addCity(@RequestBody City city) {

        int addCity = cityService.addCity(city.getCityName());
        if (addCity != 1) {
            throw new UserException("添加失败");
        }
        return Result.result().setMsg("添加成功");
    }
    /**
     * 修改城市信息
     * @return
     */
    @PostMapping("/admin/city/updateCity")
    public Result updateCity(@RequestBody City city) {
        int updateCity = cityService.updateCity(city);
        if (updateCity != 1) {
            throw new UserException("修改失败");
        }
        return Result.result().setMsg("修改成功");
    }


    @GetMapping("/admin/city/deleteCity")
    public Result deleteCity(Integer cityId) {
        int i = cityService.deleteCityById(cityId);
        if (i != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }
}
